/*
 * @Author: szx
 * @Date: 2021-11-08 14:13:27
 * @LastEditTime: 2021-11-08 15:33:24
 * @Description:
 * @FilePath: \leetcode\1-99\33\33.cpp
 */
#include <iostream>
#include <vector>
using namespace std;

class Solution {
   public:
    int search(vector<int>& nums, int target) {
        int l = 0, r = nums.size();
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] == target) return mid;
            if (nums[mid] < target) {
                if (nums[mid] < nums[l] && target >= nums[l]) {
                    r = mid;
                } else {
                    l = mid + 1;
                }
            } else {
                if (nums[mid] >= nums[l] && target < nums[l]) {
                    l = mid + 1;
                } else {
                    r = mid;
                }
            }
        }
        if (r == 0 || nums[l - 1] != target) return -1;
        return l - 1;
    }
};

int main() {
    Solution s;
    vector<int> v{1};

    cout << s.search(v, 1);
    return 0;
}